#include <stdio.h>

#include "src/ltdstr.h"
#include "src/ltlog.h"

int
main(int argc, char const** argv) {
    if (argc != 2) {
        ltlog_Log0(Error, "usage: ltnewc <filepath>");
        return 1;
    }

    char const* filepath = argv[1];

    ltdstr_String guard = {0};
    for (char const* it = filepath; *it != 0; ++it) {
        if (('A' <= *it && *it <= 'Z') || ('0' <= *it && *it <= '9')) {
            ltdstr_AppendChar(&guard, *it);
        } else if ('a' <= *it && *it <= 'z') {
            ltdstr_AppendChar(&guard, *it - 'a' + 'A');
        } else {
            ltdstr_AppendChar(&guard, '_');
        }
    }
    ltdstr_AppendChar(&guard, '_');

    FILE* writer = fopen(filepath, "w");
    if (writer == NULL) {
        ltlog_Log1(Error, "failed to open target file",
                   "path:", StringQuote(filepath));
        goto end;
    }

    fprintf(writer, "#ifndef %s\n", ltdstr_Get(&guard));
    fprintf(writer, "#define %s\n", ltdstr_Get(&guard));
    fprintf(writer, "\n");
    fprintf(writer, "#endif // %s\n", ltdstr_Get(&guard));

end:
    if (writer != NULL) {
        fclose(writer);
    }

    ltdstr_DeInit(&guard);

    return 0;
}
